<?php

class TblAcity extends CActiveRecord {
    
    public static function model($className=__CLASS__) {
        return parent::model($className);
    } 
     
    public function tableName() {
        return 'acity';
    } 

    /**
     * add record
     * if success return this new record primarykey id
     * if faile return false
     * @param unknown $tblName : 'user'
     * @param unknown $contentArr : array('user_name'=>$userName,
     * 'user_email'=>$userEmail)
     * @return unknown|boolean
     */
    public function addRecord($tblName, $contentArr) {
        $command = Yii::app()->db->createCommand();
        $result = $command->insert($tblName, $contentArr);
        if($result) {
            return Yii::app()->db->getLastInsertID();
        } else {
            return false;
        }
    }

    /**
     * delete record
     * if success return true
     * if faile return false
     * @param unknown $conditionArr : array(
     * 'select' => array('user_id', 'user_name'),
     * 'order' => 'id DESC',
     * 'condition' => 'user_id=:userId  AND user_name=:userName',
     * 'params' => array('userId'=>$userId, 'userName'=>$userName))
     * @return boolean
     */
    public function deleteRecord($conditionArr) {
        $result = $this::model()->find($conditionArr);
        if (!empty($result)) {
            if($result->delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }


    /**
     * update record
     * if success return true
     * if faile return false
     * @param unknown $conditionArr : array(
     * 'select' => array('user_id', 'user_name'),
     * 'order' => 'id DESC',
     * 'condition' => 'user_id=:userId  AND user_name=:userName',
     * 'params' => array('userId'=>$userId, 'userName'=>$userName))
     * @param unknown $contentArr : array(
     * 'userId' => $userId,
     * 'userName' => $userName
     * )
     * @return boolean
     */
    public function updateRecord($conditionArr, $contentArr) {
        $result = $this::model()->find($conditionArr);
        if (empty($result) || empty($contentArr)) {
            return false;
        }
        foreach ($contentArr as $key=>$value) {
            $result->$key = $value;
        }
        if(!$result->save()) {
            return false;
        }
        return true;
    }

    /**
     * select record
     * if success return value
     * if faile return false
     * @param array $conditionArr : array(
     * 'select' => array('user_id', 'user_name'),
     * 'order' => 'id DESC',
     * 'condition' => 'user_id=:userId  AND user_name=:userName',
     * 'params' => array('userId'=>$userId, 'userName'=>$userName))
     * @return NULL|unknown
     */
    public function selectRecord(array $conditionArr) {
        $result = $this::model()->find($conditionArr);
        if(!$result) {
            return NULL;
            exit;
        }
        return $result;
    }

    /**
     * select record all
     * if success return value
     * if faile return false
     * @param array $conditionArr : array(
     * 'select' => array('user_id', 'user_name'),
     * 'order' => 'id DESC',
     * 'condition' => 'user_id=:userId  AND user_name=:userName',
     * 'params' => array('userId'=>$userId, 'userName'=>$userName))
     * @return NULL|unknown
     */
    public function selectRecordAll(array $conditionArr) {
        $result = $this::model()->findAll($conditionArr);
        if(!$result) {
            return NULL;
            exit;
        }
        return $result;
    }
    
}
